package com.kxq.zhuoyue.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kxq.zhuoyue.pageUtilEntity.Page;
import com.kxq.zhuoyue.po.BusinessCard;
import com.kxq.zhuoyue.service.BusinessCardService;
import com.kxq.zhuoyue.util.UUID;
import com.kxq.zhuoyue.util.result.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xjh
 * @since 2023-09-01
 */
@RestController
@RequestMapping("/card")
public class BusinessCardController {

    @Autowired
    private BusinessCardService businessCardService;

    /**
     * 查询
     *
     * @param page
     * @return
     */
    @PostMapping("/byCard")
    public AjaxResult byCard(@RequestBody Page page) {
        Map map = new HashMap<>();
        LambdaQueryWrapper<BusinessCard> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(BusinessCard::getCardName, page.getCardName());
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BusinessCard> list = businessCardService.list(queryWrapper);
        PageInfo pageInfo = new PageInfo<>(list);
        map.put("data", list);
        map.put("pageSize", pageInfo.getPageSize());
        map.put("total", pageInfo.getTotal());
        return AjaxResult.success("获取成功 !", map);
    }

    /**
     * 添加
     * @param businessCard
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BusinessCard businessCard) {
        String id = UUID.getOrderIdByTime();
        businessCard.setCardStatus("未审核");
        businessCard.setBusinessCardId(id);
        boolean save = businessCardService.save(businessCard);
        if (save) {
            return AjaxResult.success("添加成功 !");
        } else {
            return AjaxResult.success("添加失败 ！");
        }
    }

    /**
     * 更新
     * @param businessCard
     * @return
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BusinessCard businessCard) {
        boolean update = businessCardService.updateById(businessCard);
        if (update) {
            return AjaxResult.success("修改成功 !");
        } else {
            return AjaxResult.success("修改失败 ！");
        }
    }

    /**
     * 删除
     * @param businessCardId
     * @return
     */
    @DeleteMapping("/delete/{businessCardId}")
    public AjaxResult delete(@PathVariable String businessCardId){
        boolean b = businessCardService.removeById(businessCardId);
        if (b) {
            return AjaxResult.success("删除成功 !");
        } else {
            return AjaxResult.success("删除失败 ！");
        }
    }
}

